package com.shiny.analysisframework.activity.base;

import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;
import com.shiny.analysisframework.R;
import org.greenrobot.eventbus.EventBus;

public class BaseActivity extends AppCompatActivity {
    //nox_adb.exe connect 127.0.0.1:62001
    //Charles
    protected static String TAG;

    protected FragmentManager fm;
    protected FragmentTransaction transaction;

    protected Runnable toastRunner = new Runnable() {
        @Override
        public void run() {
            if(null!=toast) {
                toast.cancel();
            }
        }
    };
    protected Handler toastHandler = new Handler();
    protected Toast toast;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        TAG = getComponentName().getShortClassName();
    }

    @Override
    public void onStart(){
        super.onStart();
        EventBus.getDefault().register(this);
    }

    @Override
    public void onResume(){
        super.onResume();
    }

    @Override
    protected void onPause() {
        super.onPause();
    }

    @Override
    protected void onStop() {
        EventBus.getDefault().unregister(this);
        super.onStop();
    }

    protected void showToast(String tv) {
        if( tv == null || tv.isEmpty()) {
            return;
        }
        toastHandler.removeCallbacksAndMessages(toastRunner);
        if ( null == toast ) {
            toast = Toast.makeText(this,tv,Toast.LENGTH_SHORT);
            View view = toast.getView();
            view.setBackgroundColor(Color.BLACK);
            //TextView v = (TextView)view.findViewById(R.id.textview);
            //v.setTextColor(Color.WHITE);
        }
        toast.setText(tv);
        toastHandler.postDelayed(toastRunner,2000);
        toast.show();
    }

}
